package com.pirestupppai.widgets.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.pirestupppai.R;
import com.pirestupppai.adapter.ViewPagerAdapter;
import com.pirestupppai.widgets.helper.ScaleAlphaPageTransformer;

import java.util.ArrayList;
import java.util.List;


/**
 * 首页Module
 *
 * author: xiaoluo
 * date: 2017/6/29 11:34
 */
public class ModuleView extends RelativeLayout {
    private Context mContext;

    private ViewPager mViewpager;

    private List<View> mList = new ArrayList<>();
    private ScaleAlphaPageTransformer mTransformer = new ScaleAlphaPageTransformer();

    public ModuleView(Context context) {
        this(context, null);
    }

    public ModuleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModuleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.widget_module_view, this, true);
        mViewpager = (ViewPager) findViewById(R.id.module_viewpager);

        initListeners();
    }

    private void initListeners() {
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                invalidate();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mViewpager.dispatchTouchEvent(event);
            }
        });
    }

    /**
     * 设置module数据
     */
    public void setModuleData(List<View> list) {
        if (list == null || list.size() == 0) {
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
        mList = list;
        mViewpager.setAdapter(new ViewPagerAdapter(mList));
        mViewpager.setOffscreenPageLimit(3);
        mTransformer.setType(true, true);
        mViewpager.setPageTransformer(true, mTransformer);
        mViewpager.setCurrentItem(1);
    }

    public ViewPager getViewpager() {
        return mViewpager;
    }
}
